package org.sunrise.game.rpc.function;

import lombok.Data;

import java.util.HashMap;
import java.util.Map;

@Data
public class RpcResult {
    private Object[] context;
    private Object[] data;
    private int result;
    private Map<String, Object> contextMap;
    private Map<String, Object> dataMap;

    public RpcResult() {
    }

    public void setContext(Object[] context) {
        this.context = context;
        if (context.length % 2 != 0) {
            return;
        }
        contextMap = new HashMap<>();
        for (int i = 0; i < context.length; i += 2) {
            contextMap.put((String) context[i], context[i + 1]);
        }
    }

    public Object getContext(String name) {
        return contextMap.get(name);
    }

    public Object getData(String name) {
        if (dataMap == null) {
            dataMap = new HashMap<>();
            if (data != null) {
                for (int i = 0; i < data.length; i += 2) {
                    dataMap.put((String) data[i], data[i + 1]);
                }
            }
        }
        return dataMap.get(name);
    }
}
